//
//  SubjectTableViewCell.m
//  AroundTheWorld
//
//  Created by dllo on 16/5/17.
//  Copyright © 2016年 IfelseGo an. All rights reserved.
//

#import "SubjectTableViewCell.h"
#import "SubjectModel.h"
#import "SubjectCollectionViewCell.h"


#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height

@implementation SubjectTableViewCell

//自定义初始化
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addView];
        self.smallArray = [NSMutableArray array];

    }
    return self;
}

- (void)addView
{
    self.contentView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff,0xCFCFCF);

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 30) / 2, 125);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    UICollectionView *collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
    
    
    collectionV.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:collectionV];
    
    //成为代理人
    collectionV.delegate = self;
    collectionV.dataSource = self;
    
    //注册
    [collectionV registerClass:[SubjectCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    self.myCollectionV = collectionV;
}
//cell数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}
//建cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SubjectCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    SubjectModel *model = [self.subjectArray objectAtIndex:indexPath.row + 1];
//    NSLog(@"model.********** %@", model.photo);
    [cell.smallImageV sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:nil];
    cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff,0xCFCFCF);
    return cell;
}

- (void)setSubjectArray:(NSArray *)subjectArray
{
    _subjectArray = subjectArray;
    [self.myCollectionV reloadData];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.myCollectionV.frame = self.contentView.frame;
}

//点击触发方法

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SubjectModel *model = [self.subjectArray objectAtIndex:indexPath.row + 1];
    self.myBlock(model.url, model.title);
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
